﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using DataAccess.Entities;
using Portal.Code;

namespace Portal
{
    public partial class SearchResult : BasePage
    {
        private int categoryId;
        private string kw;
        private int pageSize = Constant.DefaultPageSize;
        private int pageNum = Constant.DefaultPageNum;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            int.TryParse(Request["CategoryId"], out categoryId);
            kw = Request["Kw"];
            if (!int.TryParse(Request["PageSize"], out pageSize) || pageSize < 10)
                pageSize = Constant.DefaultPageSize;
            if (!int.TryParse(Request["PageNum"], out pageNum) || pageNum < 1)
                pageNum = Constant.DefaultPageNum;

            litPageTitle.Text = string.Format("搜索“{0}”的结果如下：", kw);

            int totalCount;
            List<Organization> orgs = null;
            using (var db = new CompanyDicContext())
            {
                var query = db.Organizations.Where(p => p.Name.Contains(kw) && (categoryId > 0 ? p.CategoryId == categoryId : true));
                totalCount = query.Count();
                orgs = query.OrderByDescending(p=>p.TotalMark).Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
            }

            var totalPageNum = int.Parse(decimal.Ceiling((decimal)totalCount / pageSize).ToString());
            Pagination pagination = PaginationHelper.GetPaginationInfo(totalPageNum, pageNum);

            InitPageContent(orgs);
            InitPagination(pagination);
        }


        private void InitPageContent(List<Organization> orgs)
        {
            var sb = new StringBuilder();
            var index = (pageNum - 1) * pageSize + 1;
            foreach (var org in orgs)
            {
                sb.AppendFormat("<li class=\"item_container float_left\">");
                sb.AppendFormat("<div class=\"rank_num float_left\">{0}</div>", index);
                sb.AppendFormat("<div class=\"item_info float_left\">");
                sb.AppendFormat("<a class=\"item_title\" href=\"Detail.aspx?Id={0}&Rank={1}\">{2}</a>", org.Id, index, org.Name);
                sb.AppendFormat("<div class=\"item_address\">地址：{0}</div>", org.Address);
                sb.AppendFormat("</div><div class=\"mark_info float_right\">");
                sb.AppendFormat("<div>总分：<span class=\"total_mark\">{0}</span></div>", org.TotalMark);
                sb.AppendFormat("<a class=\"more_link item_more\" href=\"Detail.aspx?Id={0}&Rank={1}\">了解详情</a>", org.Id, index);
                sb.AppendFormat("</div></li>");
                index++;
            }
            litListContent.Text = sb.ToString();
        }


        private void InitPagination(Pagination pagination)
        {
            var linkFormat = "SearchResult.aspx?CategoryId={0}&PageSize={1}&PageNum={2}&Kw={3}";
            var sb = new StringBuilder();
            sb.AppendFormat("<li><a href=\"{0}\"><</a></li>",
                            pagination.PrePageNum == pagination.CurPageNum
                                ? "#"
                                : string.Format(linkFormat, categoryId, pageSize, pagination.PrePageNum, kw));
            for (int i = pagination.StartPageNum; i <= pagination.EndPageNum; i++)
            {
                if (i == pagination.CurPageNum)
                {
                    sb.AppendFormat("<li><a class=\"selected\" href=\"#\">{0}</a></li>", i);
                }
                else
                {
                    sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
                                    string.Format(linkFormat, categoryId, pageSize, i, kw), i);
                }
            }
            sb.AppendFormat("<li><a href=\"{0}\">></a></li>",
                            pagination.NextPageNum == pagination.CurPageNum
                            ? "#" :
                            string.Format(linkFormat, categoryId, pageSize, pagination.NextPageNum, kw));

            litTopPagination.Text = sb.ToString();
            litBottomPagination.Text = sb.ToString();
        }
    }
}